home *** CD-ROM | disk | FTP | other *** search
- #include <OpenTransport.h>
- #include <OpenTptInternet.h>
- #include <Events.h>
- #include <stdio.h>
- #include <string.h>
-
- static UInt32 gLastTicks = 0;
-
- static pascal void MyNotifier(void* contextPtr, OTEventCode code,
- OTResult result, void* cookie)
- {
- #pragma unused(contextPtr)
- #pragma unused(result)
- #pragma unused(cookie)
-
- switch (code) {
- case kOTSyncIdleEvent:
- if ( TickCount() > gLastTicks + 10) {
- printf(".");
- fflush(stdout);
- gLastTicks = TickCount();
- }
- break;
- default:
- // do nothing
- break;
- }
- }
-
- static char *gHTTPServer = "www.stairways.com:80";
- static char *gGetHTTPCommand = "GET / HTTP/1.0\15\12\15\12";
-
- void main(void)
- {
- EndpointRef ep;
- OSStatus err;
- TCall connectCall;
- DNSAddress connectAddr;
- OTResult bytesSent;
- OTResult bytesReceived;
- char ch;
- OTFlags junkFlags;
-
- printf("Hello Cruel World!\n");
-
- err = InitOpenTransport();
- if (err == noErr) {
- ep = OTOpenEndpoint(OTCreateConfiguration("OTSessionWatcher,tcp"), 0, nil, &err);
- if (err == noErr) {
- (void) OTSetSynchronous(ep);
- (void) OTSetBlocking(ep);
- (void) OTUseSyncIdleEvents(ep, true);
- (void) OTInstallNotifier(ep, MyNotifier, 0);
- (void) OTBind(ep, nil, nil);
-
- OTMemzero(&connectCall, sizeof(connectCall));
- connectCall.addr.buf = (UInt8 *) &connectAddr;
- connectCall.addr.len = OTInitDNSAddress(&connectAddr, gHTTPServer);
-
- err = OTConnect(ep, &connectCall, nil);
-
- if (err == noErr) {
- bytesSent = OTSnd(ep, gGetHTTPCommand, strlen(gGetHTTPCommand), 0);
- if ( bytesSent < 0 ) {
- err = bytesSent;
- }
- }
- if (err == noErr) {
- do {
- bytesReceived = OTRcv(ep, &ch, 1, &junkFlags);
- if (bytesReceived < 0) {
- err = bytesReceived;
- } else {
- printf("%c", ch);
- }
- } while (err == noErr);
- }
-
- OTCloseProvider(ep);
- }
- CloseOpenTransport();
- }
-
- if (err == noErr) {
- printf("Success!\n");
- } else {
- printf("Failed with error %d.\n", err);
- }
- printf("Done. Press command-Q to Quit.\n");
- }